<?php
/**
 * 页面定义
 */

class App_Web extends Base_App
{
    protected $lgmode;          //页面逻辑模式
                                //公开页面 -- pub  (不需登录)
                                //私有页面 -- pri  (需要登录)
                                //宿主页面 -- host (需要登录)
    protected $smarty;
    protected $_uid;            //当前登陆用户的uid
    protected $allowedMethods = array('HEAD', 'GET', 'POST');

    function __get($name)
    {
        if ($name == '_user')
        {
            $this->$name = $this->_uid ? $this->user_info->getInfo($this->_uid, array('30','48','120')) : NULL;
            return $this->$name;
        }
        else if ($name === 'one')
        {
            return $this->data_one;
        }
        else if ($name === 'split')
        {
            return $this->data_split;
        }
        else if ($this->isMyObj($name))
        {
            $className = $this->getClassName($name);
            $this->$name = new $className;
            return $this->$name;
        }
        return NULL;
    }

    function __construct($lgmode)
    {
        $this->lgmode = $lgmode;
        $this->smarty = self::createSmarty();
    }

    public static function createSmarty()
    {
        $smarty = new Tool_Smarty(TEMPLATE_PATH);
        if (!defined('IN_DEV') || !IN_DEV)
        {
        	$smarty->getEngine()->loadFilter('pre', 'strip');
        }
        return $smarty;
    }

    public function setAllowedMethod($method)
    {
        $this->allowedMethods = array_map('strtoupper', (array) $method);
    }

    protected function delegateTo($path)
    {
        chdir(HTDOCS_PATH . DS . dirname($path));
        require_once HTDOCS_PATH . DS . $path;
    }

    protected function checkAuth()
    {
        if (!in_array($_SERVER['REQUEST_METHOD'], $this->allowedMethods))
        {
            header('HTTP/1.1 405 Method Not Allowed');
            header('Allow: '.implode($this->allowedMethods, ', '));
            exit;
        }

        $loginRaw = new Login_Raw();
        $this->_uid = $loginRaw->checkCookie();
        
        //检查是否被屏蔽
        if ($this->_user && Conf_User::STATUS_LOCKED == $this->_user['status'] )
        {
        	echo "ooloo";
        	exit;
        }
    }

    protected function setCommonPara()
    {
        $this->smarty->assign('_imgHost', IMG_HOST);
        $this->smarty->assign('_wwwHost', WWW_HOST);
        $this->smarty->assign('_uid', $this->_uid);
        $this->smarty->assign('_user', $this->_user);
    }

    protected function showError($ex)
    {
        $error = '['.$ex->getCode().']: '.$ex->getMessage();
        if ($ex->reason)
        {
            $error .= ' '.$ex->reason;
        }
        echo $error."\n";

        print_r($ex->getTrace());
        echo "\n";
    }

    private function isMyObj($name)
    {
        //目录
        $name = strtolower($name);
        $arr = explode('_', $name);
        if(empty($arr))
        {
            return false;
        }
        $dir = $arr[0];

        //文件
        $className = implode('_', array_map('ucfirst',$arr));
        $classFile = INCLUDE_PATH . $dir . DS . $className . '.php';
        if (is_file($classFile))
        {
            return true;
        }
        return false;
    }

    private function getClassName($name)
    {
        //目录
        $name = strtolower($name);
        $arr = explode('_', $name);
        if(empty($arr))
        {
            return false;
        }

        //文件
        $className = implode('_', array_map('ucfirst',$arr));
        return $className;
    }
}
?>